package com.penghk.springbootdemo.rabbitMQ.spring;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;

public class Consumer2 {

    public static void main(String[] args) throws InterruptedException {

        ConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1", 5672);

        //消息监听
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        FooMessageListener listener = new FooMessageListener();
        container.setMessageListener(listener);
        container.setQueueNames("springQueue");
        container.start();

        while (true) {
            Thread.sleep(1000);
            System.out.println("main thread running");
        }
    }
}

class FooMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        String messageBody = new String(message.getBody());
        System.out.println(" [x] Received '" + messageBody + "'");
    }
}